﻿#include <Windows.h>
#include <iostream>
#include <string>

#include "..\ZeroCommon\ZeroCommon.h"

int Error(const std::string msg);

int main()
{
    HANDLE hDevice = ::CreateFile(L"\\\\.\\Zero", GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, 0, nullptr);
    if (hDevice == INVALID_HANDLE_VALUE) {
        return Error("Failed to open device");
    }

    BYTE bBuffer[64];
    for (int i = 0; i < sizeof(bBuffer); ++i) {
        bBuffer[i] = i + 1;
    }

    DWORD dwBytes;
    BOOL isOK = ::ReadFile(hDevice, bBuffer, sizeof(bBuffer), &dwBytes, nullptr);
    if (isOK == FALSE) {
        return Error("Failed to read");
    }
    if (dwBytes != sizeof(bBuffer)) {
        std::cout << "Wrong number of bytes" << std::endl;
    }

    ULONG ulTotal = 0;
    for (auto n : bBuffer) {
        ulTotal += n;
    }
    if (ulTotal != 0) {
        std::cout << "Wrong data" << std::endl;
    }

	ZeroStatus* Status  = new ZeroStatus;
	DWORD dwReturnBytes = 0;
    isOK                = ::DeviceIoControl(hDevice, IOCTL_ZERO_GET_STATS, nullptr, 0, Status, sizeof(ZeroStatus), &dwReturnBytes, nullptr);
    if (isOK == FALSE) {
        return Error("DeviceIoControl error");
    }

    std::cout << "TotalRead: " << Status->TotalRead << std::endl;
    std::cout << "ReturnBytes: " << dwReturnBytes << std::endl;

    getchar();

    delete Status;
    CloseHandle(hDevice);

    return 0;
}

int Error(const std::string msg)
{
    std::cout << msg << ": error: " << GetLastError() << std::endl;
    getchar();
    return 1;
}